package com.jxb.lession78.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jxb.lession78.common.Constant;
import com.jxb.lession78.dao.EmpMapper;
import com.jxb.lession78.entity.Emp;
import com.jxb.lession78.services.EmpServices;
import com.jxb.lession78.util.MybatisUtil;

import java.util.List;
import java.util.Map;

public class EmpServicesImpl implements EmpServices {
    @Override
    public Emp getEmpByEmpname(String username) {
        try {
            EmpMapper mapper = MybatisUtil.getMapper(EmpMapper.class);
            Emp emp = mapper.getEmpByUsername(username);
            if (emp != null) {
                MybatisUtil.commit();
                return emp;
            }
        } catch (Exception e) {
            e.getStackTrace();
        }
        MybatisUtil.rollback();
        return null;
    }

    @Override
    public List<Emp> getAllEmp() {
        try {
            EmpMapper mapper = MybatisUtil.getMapper(EmpMapper.class);
//            PageHelper.startPage(pageNum,5);
            List<Emp> empList = mapper.getAllEmp();
//            PageInfo<Emp> info = new PageInfo<>(empList);
            return empList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        MybatisUtil.rollback();
        return null;
    }

    @Override
    public PageInfo<Emp> getEmpByMap(Map<String, String> map, int pageNum) {
        try {
            EmpMapper mapper = MybatisUtil.getMapper(EmpMapper.class);
            PageHelper.startPage(pageNum,5);
            List<Emp> empList = mapper.getEmpByMap(map);
            PageInfo<Emp> info = new PageInfo<>(empList);
            MybatisUtil.commit();
            return info;
        } catch (Exception e) {
            e.printStackTrace();
        }
        MybatisUtil.rollback();
        return null;
    }

    @Override
    public boolean addEmp(Emp emp) {
        try {
            EmpMapper mapper = MybatisUtil.getMapper(EmpMapper.class);
            int i = mapper.addEmp(emp);
            if (i > 0){
                MybatisUtil.commit();
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        MybatisUtil.rollback();
        return false;
    }

    @Override
    public Emp getEmpByEno(int eno) {
        EmpMapper mapper = MybatisUtil.getMapper(EmpMapper.class);
        Emp emp = mapper.getEmpByEno(eno);
        if (emp != null){
            MybatisUtil.commit();
            return emp;
        }
        MybatisUtil.rollback();
        return null;
    }

    @Override
    public boolean deleteEmp(int eno) {
        EmpMapper mapper = MybatisUtil.getMapper(EmpMapper.class);
        int i = mapper.deleteEmp(eno);
        if (i > 0){
            MybatisUtil.commit();
            return true;
        }
        MybatisUtil.rollback();
        return false;
    }

    @Override
    public boolean updateEmp(Emp emp) {
        EmpMapper mapper = MybatisUtil.getMapper(EmpMapper.class);
        int i = mapper.updateEmp(emp);
        if (i > 0) {
            MybatisUtil.commit();
            return true;
        }
        MybatisUtil.rollback();
        return false;
    }
}
